package by.bucha.testproject.fragments;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListFragment;

import by.bucha.tabnavigationproject.R;
import by.bucha.tabnavigationproject.tabsframework.AbstractTabActivity;
import by.bucha.testproject.adapters.PeopleListAdapter;
import by.bucha.testproject.adapters.PeopleListAdapter.ViewHolder;
import by.bucha.testproject.content.contract.TaskContract;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class PersonalListFragment extends SherlockListFragment implements
		LoaderManager.LoaderCallbacks<Cursor>, OnClickListener {

	public PersonalListFragment() {
	}

	public AbstractTabActivity getTabStack() {
		return (AbstractTabActivity) getActivity();
	}

	@Override
	public void onAttach(Activity activity) {
		Log.d(AbstractTabActivity.TAG, "onAttach in fragment "
				+ getClass().getName());
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(AbstractTabActivity.TAG, "onCreate in fragment "
				+ getClass().getName());
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onDestroy() {
		Log.d(AbstractTabActivity.TAG, "onDestroy in fragment "
				+ getClass().getName());
		super.onDestroy();
	}

	@Override
	public void onDetach() {
		Log.d(AbstractTabActivity.TAG, "onDetach in fragment "
				+ getClass().getName());
		super.onDetach();
	}

	@Override
	public void onStart() {
		Log.d(AbstractTabActivity.TAG, "onStart in fragment "
				+ getClass().getName());
		super.onStart();
	}

	@Override
	public void onStop() {
		Log.d(AbstractTabActivity.TAG, "onStop in fragment "
				+ getClass().getName());
		super.onStop();
	}

	private PeopleListAdapter mAdapter;

	private static final int LOADER_ID = 0;

	public static final String DETAILS_BACKSTACK_NAME = "details";

	public static final String ADD_BACKSTACK_NAME = "add";

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(AbstractTabActivity.TAG, "onCreateView in fragment "
				+ getClass().getName());
		setHasOptionsMenu(true);

		mAdapter = new PeopleListAdapter(getActivity().getApplicationContext(),
				null);

		setListAdapter(mAdapter);

		getLoaderManager().initLoader(LOADER_ID, null,
				PersonalListFragment.this);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onListItemClick(ListView listView, View view, int position,
			long id) {
		super.onListItemClick(listView, view, position, id);

		ViewHolder idHolder = (ViewHolder) view.getTag();
		int modelNumber = idHolder.getModelId();

		changeFragmentToLeaderDetails(modelNumber);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int paramInt, Bundle paramBundle) {

		return new CursorLoader(getActivity().getApplicationContext(),
				TaskContract.WorkerColumns.CONTENT_URI, null, null, null,
				TaskContract.WorkerColumns.NAME);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> paramLoader, final Cursor cursor) {
		if (cursor.getCount() != 0) {
			mAdapter.swapCursor(cursor);
		} else {

			String warning = getActivity()
					.getApplicationContext()
					.getResources()
					.getString(
							R.string.personal_list_fragment_strings_empty_cursor);
			Toast.makeText(getActivity().getApplicationContext(), warning,
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> cursor) {
		mAdapter.swapCursor(null);

	}

	@Override
	public void onResume() {
		super.onResume();
		showMyOwnCustomView();
		Log.d(AbstractTabActivity.TAG, "onResume in fragment "
				+ getClass().getName());
	}

	@Override
	public void onPause() {
		super.onPause();
		hideMyOwnCustomView();
		Log.d(AbstractTabActivity.TAG, "onPause in fragment "
				+ getClass().getName());
	}

	private void changeFragmentToAddPerson() {

		AddPersonFragment newFragment = new AddPersonFragment();

		getTabStack().pushFragment(newFragment);
	}

	private void changeFragmentToLeaderDetails(int id) {

		// Create fragment and give it an argument specifying the article it
		// should show

		PersonDetailsFragment newFragment = new PersonDetailsFragment();

		Bundle arguments = new Bundle();

		arguments.putInt(PersonDetailsFragment.EXTRA_MODEL_ID, id);
		newFragment.setArguments(arguments);

		getTabStack().pushFragment(newFragment);
	}

	private void hideMyOwnCustomView() {
		ActionBar actionBar = getSherlockActivity().getSupportActionBar();
		actionBar.getCustomView().setVisibility(View.GONE);
	}

	private void showMyOwnCustomView() {
		LayoutInflater inflator = (LayoutInflater) getSherlockActivity()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflator.inflate(
				R.layout.action_bar_menu_items_fragment_personal_list, null);
		ActionBar actionBar = getSherlockActivity().getSupportActionBar();
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setCustomView(v);

		setOnClickListenerToItems(actionBar.getCustomView());
	}

	private void setOnClickListenerToItems(View v) {

		ImageButton buttonAddPerson = (ImageButton) v
				.findViewById(R.id.action_bar_menu_items_fragment_personal_list_item_add);

		buttonAddPerson.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.action_bar_menu_items_fragment_personal_list_item_add:
			changeFragmentToAddPerson();
			break;
		default:
			break;
		}
	}
}
